<?php
session_start(); //Do not remove this
	if (!isset($_SESSION['CREATED'])) {
		$_SESSION['CREATED'] = time();
	} else if (time() - $_SESSION['CREATED'] > 600) {
		// session started more than 10 minutes ago
									//sau 10' thì thay đổi giá trị SESSION['login'] để bảo mật
			session_unset();     	// unset $_SESSION variable for this page
			session_destroy();   	// destroy session data
	}
	

    if ($_SESSION['login']!='123!@#'){
		header('location:index.php');
	}
	
	
	require "../php/config.php";
	require "../php/libs/db_functions.php";
	$story_id =  isset($_GET['story_id'])? $_GET['story_id'] : 1;
	CONNECT_DB();
    mysql_query("SET NAMES utf8");
	
	$sql = "SELECT story_id, story_name FROM story_list";
	//$result2 = mysql_query($myQuery) or die($myQuery."<br/><br/>".mysql_error());
	$result=mysql_query($sql) or die($sql."<br/><br/>".mysql_error());
	$storylist=array();
	$indexi=0;
	while($rows=mysql_fetch_array($result)){
      $storylist[$indexi]=$rows;
	  $indexi++;
    }
	//echo '<pre>';
	//print_r($storylist);
	//echo '</pre>';
?>
<!doctype html>
<html>
	<head>
		<title>Admin control panel</title>
		<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
		<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
	</head>
<body>
	<div id="login_div">
		<p><span>Tên truyện: </span><select id='storyname'>
		<?
			foreach($storylist as $story_list){
				echo '<option value='.$story_list['story_id'].'>'.$story_list['story_name'].'</option>';
			}	
		?>
		</select></p>
		<p><span>Tên chương</span><input type="text" name="chapter_name" id="chapter_name"></p>
		<p><span>Nội dung</span>
			<textarea rows="8" cols="50" id="chapter_content">
			</textarea>
		</p>
		<button type="button" id = "insert">Insert</button>
		<br />
		<button type="button" id = "logout">Logout</button>
	</div>
	<script>
		$('#insert').click(function(){
			var story_id = $('#storyname').val();
			var chapter_name = $('#chapter_name').val();
			var chapter_content = $('#chapter_content').val();
			//alert (story_id);
			//alert (chapter_name);
			//alert (chapter_content);
			
			$.post('ajax/insertchapter.php',{chapter_name:chapter_name, chapter_content: chapter_content, story_id: story_id}, function(data){
						alert (data);
						//if (data=='logged in'){
						//	window.location.href='main.php';
						//}
			});
			
		});
		$('#logout').click(function(){
			alert('logout');
			window.location.href='logout.php';
		});
	</script>
</body>
</html>